/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.view;

import com.controller.BeberageManager;
import com.domain.Beberage;
import com.gui.ShowBeberageInformationForm;
import java.awt.Dialog.ModalityType;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 *
 * @author Miguel
 */
public class ShowBeberageInformationView implements ActionListener{
    
    public ShowBeberageInformationView(Beberage viewingBeberage){
        modifyingBeberage = viewingBeberage;
        initializeBeberageManager();
        initializeBeberageInfoForm();
        initializeButtons();
        initializeTextFields();
    }
    
    public void showBeberageInformationForm(){
        beberageInfoForm.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String buttonClicked = e.getActionCommand();
        switch(buttonClicked){
            case MODIFY_BEBERAGE:
                modifyBeberage();
                break;
            case CLOSE_INFO_FORM:
                closeBeberageInformationView();
                break;
        }
    }
    
    public void closeInformationForm(){
        beberageInfoForm.dispose();
    }
    
    public void showMessageToForm(String msg){
        JOptionPane.showMessageDialog(beberageInfoForm,msg,"Mensaje",JOptionPane.INFORMATION_MESSAGE);
    }
    
    public void closeBeberageInformationView(){
        beberageInfoForm.dispose();
    }
    
    private void initializeBeberageManager(){
        beberageManager = BeberageManager.getInstance();
    }
    
    private void modifyBeberage(){
        Beberage newBeberage = getModifiedBeberage();
        beberageManager.modifyBeberage(modifyingBeberage,newBeberage,this);
    }
    
    private Beberage getModifiedBeberage(){
        Beberage modifiedBeberage = new Beberage();
        
        String beberageNewName = beberageNameTextField.getText();
        modifiedBeberage.setName(beberageNewName);
        
        String beberageNewDescription = beberageDescriptionTextArea.getText();
        modifiedBeberage.setDescription(beberageNewDescription);
        
        return modifiedBeberage;
    }
    
    private void initializeButtons(){
        modifyBeberageButton = beberageInfoForm.getModifyBeberageButton();
        modifyBeberageButton.setActionCommand(MODIFY_BEBERAGE);
        modifyBeberageButton.addActionListener(this);
        
        closeInfoFormButton = beberageInfoForm.getCancelModificationButton();
        closeInfoFormButton.setActionCommand(CLOSE_INFO_FORM);
        closeInfoFormButton.addActionListener(this);
    }
    
    private void initializeBeberageInfoForm(){
        beberageInfoForm = new ShowBeberageInformationForm();
        beberageInfoForm.setModalityType(ModalityType.TOOLKIT_MODAL);
        beberageInfoForm.setTitle("Información de Bebidas");
        beberageInfoForm.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    }
    
    private void initializeTextFields(){
        beberageNameTextField = beberageInfoForm.getBeberageNameTextField();
        beberageNameTextField.setText(modifyingBeberage.getName());
        
        beberageDescriptionTextArea = beberageInfoForm.getBeberageDescriptionTextArea();
        beberageDescriptionTextArea.setText(modifyingBeberage.getDescription());
    }
    private JButton modifyBeberageButton;
    private JButton closeInfoFormButton;
    private JTextField beberageNameTextField;
    private JTextArea beberageDescriptionTextArea;
    
    private final String MODIFY_BEBERAGE = "MODIFY_BEBERAGE";
    private final String CLOSE_INFO_FORM = "CLOSE_FORM";
    
    private Beberage modifyingBeberage;
    private BeberageManager beberageManager;
    
    private ShowBeberageInformationForm beberageInfoForm;

}
